package br.com.puertorico.mecanismos.ui.converters;

import java.math.BigDecimal;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import javax.faces.convert.NumberConverter;

import br.com.puertorico.common.utils.Formatador;

/**
 * Respons&aacute;vel pela conver&ccedil;&atilde;o de um valor float.
 *
 * @author <a href="mailto:daniel.camargo@ufms.br">Daniel Camargo</a>
 * @version $id$
 */
public class FloatConverter extends NumberConverter {

	/**
	 * Construtor da classe FloatConverter.
	 */
	public FloatConverter() {
		super();
		super.setLocale(Formatador.getLocaleBrasil());
	}

	/**
	 * {@inheritDoc}
	 */
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		if (!value.equals("")) {
			try {
				String novoValor = value.replaceAll(",", ".");
				return new BigDecimal(novoValor);
			} catch (Exception e) {
				throw new ConverterException();
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		if (arg2 instanceof Number && arg2 != null) {
			return arg2.toString();
		}
		return "";
	}
}
